<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019-07-31
 * Time: 10:23
 */

namespace App\Services\Admin;

use App\Repositories\PermissionRepository;
use App\Repositories\UserRepository;
use App\Services\BaseAdminService;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Auth;

/**
 * 遵循SOLID原则（分割、单元测试、mock测试）
 * 涉及到permission的外部行为逻辑:例如角色分配、权限分配、统计等外部服务和逻辑处理
 * 该逻辑将注入到 @class PermissionController
 * Class PermissionController
 * @package app\Services\Admin
 */
class PermissionService extends BaseAdminService
{
    /**
     * @var PermissionRepository 注入的PermissionRepository
     */
    protected $permissionRepository;

    /**
     * @var UserRepository 注入的UserRepository
     */
    protected $userRepository;

    /**
     * PermissionService constructor.
     * @param PermissionRepository $permissionRepository
     */
    public function __construct(PermissionRepository $permissionRepository, UserRepository $userRepository){
        $this->permissionRepository = $permissionRepository;
        $this->userRepository = $userRepository;
    }

    /**
     * 获取权限路由列表
     * @param array $arrData
     * @return mixed
     */
    public function getPermissionRouterPage(array $arrData, $limit){

        return $this->permissionRepository->getPermissionRouterPage($arrData, $limit);
    }

    /**
     * 处理获取权限路由列表参数
     * @param array $data
     * @return array
     */
    public function handleCondition(array $data){
        $arrData = [];

        data_get($data, 'name') && array_push($arrData, ['name', 'like', '%'. $data['name'] . '%']);
        data_get($data, 'id') && array_push($arrData, ['id', $data['id']]);
        Arr::has($data, 'parent_id') && array_push($arrData, ['parent_id', $data['parent_id']]);
        Arr::has($data, 'type') && array_push($arrData, ['type', $data['type']]);

        return $arrData;
    }

    /**
     * 保存权限路由
     * @param array $addData
     * @return bool
     */
    public function addPermissionRouter(array $addData){
        return $this->permissionRepository->addPermissionRouter($addData);
    }

    /**
     * 更新权限
     * @param array $updateData
     * @return bool
     */
    public function updatePermissionRouter(array $updateData){
        return $this->permissionRepository->updatePermissionRouter($updateData);
    }

    /**
     * 删除路由权限
     * @param array $ids
     * @return int
     */
    public function deletePermissionRouter(array $ids){
        return $this->permissionRepository->deletePermissionRouter($ids);
    }

    /**
     * 获取当前用户权限
     * @return mixed
     */
    public function currentPermission(){
        return Auth::user()->getAllPermissions()->groupBy('type');
    }

    /**
     * 获取所有权限路由列表
     * @param array $data
     * @return mixed
     */
    public function choicePermission(array $data){
        return $this->permissionRepository->choicePermission($data);
    }

    /**
     * 获取单条权限路由数据
     * @param int $id
     * @return \Illuminate\Database\Eloquent\Collection|\Illuminate\Database\Eloquent\Model|static|static[]
     */
    public function singlePermission(int $id){
        return $this->permissionRepository->singlePermission($id);
    }

    /**
     * 结构性数据
     * @return \Illuminate\Database\Eloquent\Collection|static[]
     */
    public function structurePermission(){
        return $this->permissionRepository->structurePermission();
    }
}